package com.sgb.kjwl.view.ui.activity.house;

import android.content.Context;
import android.content.Intent;

import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;


import androidx.databinding.DataBindingUtil;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityLeaseInviteBinding;
import com.sgb.kjwl.di.machine.DaggerMachineComponent;
import com.sgb.kjwl.di.machine.MachineModule;
import com.sgb.kjwl.model.entity.CompanyInvitationEntity;
import com.sgb.kjwl.viewmodel.machine.LeaseInviteModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 租赁市场-邀请
 */
public class LeaseInviteActivity extends AppActivity {
    @Inject
    LeaseInviteModel viewModel;
    private ActivityLeaseInviteBinding binding;
    private String inviteCompanyNo;//被邀请单位
    private String inviteCompanyName;//被邀请单位
    private String myCompanyName;//自己的企业
    private String contact;
    private String phone;

    private List<CompanyInvitationEntity.ParamDtosBean> viewList = new ArrayList<>();
    private CompanyInvitationEntity invitationEntity = new CompanyInvitationEntity();

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMachineComponent.builder().appComponent(appComponent).machineModule(new MachineModule(this)).build().inject(this);
    }

    public static void start(Context context, String inviteCompanyNo, String inviteCompanyName) {
        Intent intent = new Intent();
        intent.putExtra("inviteCompanyNo", inviteCompanyNo);
        intent.putExtra("inviteCompanyName", inviteCompanyName);
        intent.setClass(context, LeaseInviteActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_lease_invite);
        binding.setViewModel(viewModel);
        binding.topLayout.titleView.setText("邀请");
        binding.topLayout.rightView.setText("确定");
        binding.topLayout.ivBack.setOnClickListener(v -> finish());
        binding.topLayout.rightView.setOnClickListener(v -> validateData());//立即发布按钮
        binding.tvAdd.setOnClickListener(v -> addView());
        binding.tvDel.setOnClickListener(v -> delView());
        binding.itemTvPhone.setInputType(InputType.TYPE_CLASS_NUMBER);
    }

    @Override
    protected void init() {
        inviteCompanyNo = getIntent().getStringExtra("inviteCompanyNo");
        inviteCompanyName = getIntent().getStringExtra("inviteCompanyName");
        myCompanyName = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NAME, "");
        contact = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.REAL_NAME, "");
        phone = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_PHONE, "");
        binding.itemTvCompanyMy.setContent(myCompanyName);
        binding.itemTvCompany.setContent(inviteCompanyName);
        binding.itemTvContact.setContent(contact);
        binding.itemTvPhone.setContent(phone);
    }

    private void addView() {
        if (binding.llDescGroup.getChildCount() >= 5) {
            return;
        }
        View view = View.inflate(getActivity(), R.layout.layout_lease_invite_item, null);
        binding.llDescGroup.addView(view, binding.llDescGroup.getChildCount());
        binding.tvDel.setVisibility(View.VISIBLE);
    }

    private void delView() {
        if (binding.llDescGroup.getChildCount() > 0) {
            binding.llDescGroup.removeViewAt(binding.llDescGroup.getChildCount() - 1);
            if (binding.llDescGroup.getChildCount() == 0) {
                binding.tvDel.setVisibility(View.GONE);
            }
        }

    }

    public boolean getViewList() {
        viewList.clear();
        for (int i = 0; i < binding.llDescGroup.getChildCount(); i++) {
            View view = binding.llDescGroup.getChildAt(i);
            String title = ((ItemInputTextView) view.findViewById(R.id.item_desc_title)).getContent();
            String content = ((EditText) view.findViewById(R.id.tv_desc_content)).getText().toString();
            if (TextUtils.isEmpty(title) || TextUtils.isEmpty(content)) {
                MToast.showToast(this, "请输入标题和描述");
                return false;
            }
            CompanyInvitationEntity.ParamDtosBean bean = new CompanyInvitationEntity.ParamDtosBean();
            bean.setTitle(title);
            bean.setContent(content);
            viewList.add(bean);
        }
        return true;
    }

    private void validateData() {
        if (TextUtils.isEmpty(binding.itemTvContact.getContent())) {
            MToast.showToast(this, "请输入联系人");
            return;
        } else if (TextUtils.isEmpty(binding.itemTvPhone.getContent())) {
            MToast.showToast(this, "请输入联系电话");
            return;
        }
        invitationEntity.setCompanyNo(inviteCompanyNo);
        invitationEntity.setContacts(binding.itemTvContact.getContent());
        invitationEntity.setPhone(binding.itemTvPhone.getContent());
        invitationEntity.setInviteContent(binding.tvDescContent.getText().toString());
        if (getViewList()) {
            invitationEntity.setParamDtos(viewList);
        }
        invitationEntity.setCompanyName(inviteCompanyName);
        viewModel.addInvitation(invitationEntity);
    }


}
